package com.xqy.controller;

import com.xqy.entity.UserEntity;
import com.xqy.service.UserService;
import com.xqy.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * description: UserController
 * date: 2020/9/22 15:38
 * author: ASUS
 * version: 1.0
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService service;
    @Autowired
    private RedisTemplate redisTemplate;
    @RequestMapping("/show")
    public String testShow(){
        return "user";
    }
    /**
     * description: 查询所有用户
     * version: 1.0 
     * date: 2020/9/22 17:23 
     * author: ZhouLiHao
     * 
     * @return java.util.List<com.xqy.entity.UserEntity>
     */
    @RequestMapping("/userlist")
    @ResponseBody
    public List<UserEntity> showUser(){
        if(redisTemplate.opsForValue().get("usershow")==null) {//如果缓存里面没有数据就从数据库中拉取
            System.out.println("redis缓存没有数据");
            redisTemplate.opsForValue().set("usershow", service.selectUserShow());
            return (List<UserEntity>) redisTemplate.opsForValue().get("usershow");
        }else{//如果缓存中有数据直接返回即可
            return (List<UserEntity>) redisTemplate.opsForValue().get("usershow");
        }
    }
    /**
     * description: 查询单个用户 
     * version: 1.0 
     * date: 2020/9/22 17:23 
     * author: ZhouLiHao
     * 
     * @return com.xqy.entity.UserEntity
     */ 
    @RequestMapping("/findOne")
    @ResponseBody
    public UserEntity findOne(int id){
        if (redisTemplate.opsForValue().get(""+id+"")==null) {//查询单个用户
            System.out.println("数据库没有这个用户的数据");
            redisTemplate.opsForValue().set(""+id+"",service.findone(id));
            return (UserEntity) redisTemplate.opsForValue().get(""+id+"");
        }else{
            return (UserEntity) redisTemplate.opsForValue().get(""+id+"");
        }
    }
    /**
     * description: 新增 
     * version: 1.0 
     * date: 2020/9/22 17:22 
     * author: ZhouLiHao
     * 
     * @return boolean
     */ 
    @RequestMapping("/add")
    @ResponseBody
    public  String add(@RequestBody UserEntity entity){
        System.out.println(entity.getName());
        if(service.addUser(entity)){
            redisTemplate.opsForValue().set("usershow", service.selectUserShow());//新增数据之后刷新缓存
            return "新增成功" ;
        }else{
            return "新增失败"  ;
        }

    }
    /**
     * description: 更新 
     * version: 1.0 
     * date: 2020/9/22 17:22 
     * author: ZhouLiHao
     * 
     * @return boolean
     */ 
    @RequestMapping("/userup")
    @ResponseBody
    public  String up(@RequestBody UserEntity entity){
        if(service.updateUser(entity)){
            redisTemplate.opsForValue().set("usershow", service.selectUserShow());//更新数据之后刷新缓存
            redisTemplate.opsForValue().set(""+entity.getId()+"",service.findone(entity.getId()));//根据单个用户更新之后刷新缓存
            return "修改成功" ;
        }else{
            return "修改失败"  ;
        }

    }
    /**
     * description: 批量删除用户
     * version: 1.0 
     * date: 2020/9/22 17:38 
     * author: ZhouLiHao
     *
     * @return 
     */
    @RequestMapping("/dele")
    @ResponseBody
    public String deleteUser(int[]ids){
        if(service.deleteUser(ids)){
            redisTemplate.opsForValue().set("usershow", service.selectUserShow());//删除数据之后刷新缓存
            return "删除成功" ;
        }else{
            return "删除失败"  ;
        }
    }
}
